package com.zgs.androidopengl.render

import android.content.Context
import android.opengl.GLES20
import android.opengl.GLES20.GL_FLOAT
import android.opengl.GLES20.GL_LINES
import android.opengl.GLES20.GL_POINTS
import android.opengl.GLES20.GL_TRIANGLES
import android.opengl.GLES20.glDrawArrays
import android.opengl.GLES20.glEnableVertexAttribArray
import android.opengl.GLES20.glUniform4f
import android.opengl.GLES20.glUseProgram
import android.opengl.GLES20.glVertexAttribPointer
import android.opengl.GLSurfaceView
import android.util.Log
import com.zgs.androidopengl.MainActivity
import com.zgs.androidopengl.R
import com.zgs.androidopengl.util.LoggerConfig
import com.zgs.androidopengl.util.ShaderHelp
import com.zgs.androidopengl.util.TextResourceReader
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.FloatBuffer
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

class AirHockeyRender(private val mContext: Context) : GLSurfaceView.Renderer {
    private val vertexData: FloatBuffer
    private var program: Int? = null
    private val U_COLOR: String = "u_Color"
    private var uColorLocation: Int? = null
    private val A_POSITION: String = "a_Position"
    private var aPositionLocation: Int? = null

    init {
//        val tableVerticesWithTriangles = floatArrayOf(
//            //triangle 1
//            0f, 0f,
//            9f, 14f,
//            0f, 14f,
//
//            //triangle 2
//            0f, 0f,
//            9f, 0f,
//            9f, 14f,
//
//            //line 1
//            0f, 7f,
//            9f, 7f,
//
//            //Mallets
//            4.5f, 2f,
//            4.5f, 12f
//        )
        //opengl的坐标
        /**
         * opengl的坐标系是在[-1,1]之间
         * 左下是(-1,-1)
         * 右上是(1,1)
         * 左上是(-1,1)
         * 右下是(1,-1)
         */
        val tableVerticesWithTriangles = floatArrayOf(
            //Triangle 1
            -0.5f, -0.5f,
            0.5f, 0.5f,
            -0.5f, 0.5f,

            //Triangle 2
            -0.5f, -0.5f,
            0.5f, -0.5f,
            0.5f, 0.5f,

            //Line1
            -0.5f, -0f,
            0.5f, 0f,

            //Mallets
            0.0f, -0.25f,
            0f, 0.25f,

            )

        vertexData = ByteBuffer.allocateDirect(tableVerticesWithTriangles.size * BYTES_PER_FLOAT)
            .order(ByteOrder.nativeOrder()).asFloatBuffer()

        vertexData.put(tableVerticesWithTriangles)
    }

    override fun onSurfaceCreated(gl: GL10, config: EGLConfig) {
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f)
        val vertexShaderSource =
            TextResourceReader.readTextFileFromResource(mContext, R.raw.simple_vertex_shader)
        val fragmentShaderSource =
            TextResourceReader.readTextFileFromResource(mContext, R.raw.simple_fragment_shader)

        val vertexShaderHandle = ShaderHelp.compileVertexShader(vertexShaderSource)
        val fragmentShaderHandle = ShaderHelp.compileFragmentShader(fragmentShaderSource)

        program = ShaderHelp.linkProgram(vertexShaderHandle, fragmentShaderHandle)

        if (LoggerConfig.ON) {
            ShaderHelp.validateProgram(program!!)
        }

        glUseProgram(program!!)

        uColorLocation = GLES20.glGetUniformLocation(program!!, U_COLOR)

        aPositionLocation = GLES20.glGetAttribLocation(program!!, A_POSITION)

        vertexData.position(0)

        glVertexAttribPointer(
            aPositionLocation!!,
            POSITION_COMPONENT_COUNT,
            GL_FLOAT,
            false,
            0,
            vertexData
        )

        glEnableVertexAttribArray(aPositionLocation!!)
    }

    override fun onSurfaceChanged(gl: GL10, width: Int, height: Int) {
        GLES20.glViewport(0, 0, width, height)
    }

    override fun onDrawFrame(gl: GL10) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)

        //绘制两个矩形
        glUniform4f(uColorLocation!!, 1.0f, 1.0f, 1.0f, 1.0f)
        glDrawArrays(GL_TRIANGLES, 0, 6)
        //绘制分隔线
        glUniform4f(uColorLocation!!, 1.0f, 0.0f, 0.0f, 1.0f)
        glDrawArrays(GL_LINES, 6, 2)
        //绘制木槌 蓝色
        glUniform4f(uColorLocation!!, 0.0f, 0.0f, 1.0f, 1.0f)
        glDrawArrays(GL_POINTS, 8, 1)

        //绘制木槌 红色
        glUniform4f(uColorLocation!!, 0.0f, 1.0f, 0.0f, 1.0f)
        glDrawArrays(GL_POINTS, 9, 1)


    }

    companion object {
        private const val POSITION_COMPONENT_COUNT = 2
        private const val BYTES_PER_FLOAT = 4
    }
}
